package com.taobaolite.item;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.websocket.server.PathParam;
import java.util.List;

@RestController
@RequestMapping(path = "/api/v1/items")
public class ItemController {
    private final ItemService itemService;

    @Autowired
    public ItemController(ItemService itemService) {
        this.itemService = itemService;
    }

    @GetMapping(path = "{itemId}")
    public ItemBody getItem(
            @PathVariable Long itemId,
            @RequestParam(required = false) Long userId) {
        return itemService.getItem(itemId, userId);
    }

    @GetMapping
    public List<ItemBody> getItems(
            @RequestParam(required = false) Long storeId,
            @RequestParam(required = false) Long type) {
        return itemService.getItems(storeId, type);
    }

    @PostMapping
    public ItemBody addItem(@Valid @RequestBody ItemBody itemBody) {
        return itemService.addItem(itemBody);
    }

    @PutMapping // Do NOT use Apifox to test this API! It fails to send a valid CSRF token here.
    public ItemBody updateItem(@RequestParam("itemId") Long itemId, @Valid @RequestBody ItemBody itemBody) {
        itemService.updateItem(itemBody);
        return itemBody;
    }

    @DeleteMapping
    public void deleteItem(@RequestParam("itemId") Long itemId) {
        itemService.deleteItem(itemId);
    }

}
